import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.*;


public class odbcConnectionChecker {
	
	public static void main(String[] args) {
		if (!SystemTray.isSupported()) {
			System.out.println("SystemTray is not supported");
			return;
		}
		URL imageURL = odbcConnectionChecker.class.getResource("images/teradata.jpg");
		TrayIcon trayIcon = new TrayIcon ((new ImageIcon(imageURL)).getImage());
        SystemTray tray = SystemTray.getSystemTray();

		try {
			tray.add(trayIcon);
		} catch (AWTException e) {
			e.printStackTrace();
		}        
		
		while(true) {
			trayIcon.setPopupMenu(getPopUpMenu(args, trayIcon, tray));
			try {
				Thread.sleep(60000*30); //Half hour		
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}	
	
	private static String odbc;
	
	private static PopupMenu getPopUpMenu(final String[] args, final TrayIcon ti, final SystemTray t) {
		PopupMenu popup = new PopupMenu();
		
		MenuItem odbcItem;
		
		for (String s : args) { 
				odbc = s;
				if (getConnection(s)) {
					odbcItem = new MenuItem (s+" -- OK");
					odbcItem.addActionListener ( 
						new ActionListener() { 
							public void actionPerformed(ActionEvent e) { 
								try {
									Runtime.getRuntime().exec("C:\\Program Files\\Teradata\\Client\\13.10\\Teradata SQL Assistant\\SQLA.EXE -c " + odbc);
								} catch (IOException e1) {
									e1.printStackTrace();
								}
							}
						}
					);
					
					popup.add(odbcItem);
					ti.displayMessage("Connection Status",s + " -- OK", TrayIcon.MessageType.NONE);
				} else { 
					popup.add(new MenuItem (s+ " -- Failed"));
					ti.displayMessage("Connection Status",s + " -- Failed", TrayIcon.MessageType.NONE);
				}
				try {
					Thread.sleep(2000);		
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
		}
		
		popup.addSeparator();
		
		MenuItem refreshItem = new MenuItem ("Refresh");
		popup.add(refreshItem);
		refreshItem.addActionListener(
			new ActionListener() { 
				public void actionPerformed(ActionEvent e) { 
					ti.setPopupMenu(getPopUpMenu(args, ti, t));
				}
			}
				
		);
		
		MenuItem exitItem = new MenuItem ("Exit");
		popup.add(exitItem);
		exitItem.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
						t.remove(ti);
						System.exit(0);
				}
			}
		);
		return popup;	
	}
	
	private static boolean getConnection (String dsn) {
		Connection con = null;
		try	{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			String url = "jdbc:odbc:"+dsn;
			con = DriverManager.getConnection(url);	
		} catch (ClassNotFoundException e) {
			return false;
		} catch (SQLException e) {
			return false;
		}
		try {
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return true;
	}
	
}
